package com.gxa.worker.recycling.controller;

import com.gxa.core.recycling.common.JsonResult;
import com.gxa.core.recycling.util.OOSUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * @author R
 * @data 2021-10-21 15:49
 */
@CrossOrigin
@RestController
@RequestMapping("/upload")
//@Api(value = "轮播操作接口")
public class UploadController {

    @PostMapping(value = "/uploadImage")
    public Map<String, Object> upload(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) throws IOException {
        //QQ截图20200724153059.png
        String originalFilename = multipartFile.getOriginalFilename();
        System.out.println("originalFilename:"+originalFilename);
        // 从原始文件名中获取后缀,作为新添加的文件的文件名后缀(匹配)
        String subfix = originalFilename.substring(originalFilename.indexOf(".")+1);
        System.out.println("subfix:"+subfix);
        //使用UUID作为文件名,保证服务器中存放的文件名唯一

        String targetFile = UUID.randomUUID().toString().replace("-","")+"."+subfix;
        System.out.println("targetFile:"+targetFile);


        String fileName = "d:/images/uploadImg/"+targetFile;
        System.out.println("fileName:"+fileName);

        // 构建文件对象
        File uploadFile = new File(fileName);
        //核心操作(含有异常操作, 利用异常设定状态码(和前端约定)
        // 此处应当和ExceptionController结合, 当try执行成功则设定状态码0
        // 出现异常则设定其他
        // 再将异常信息结合BusinessException抛出, 交给ExceptionController处理
        try {
            multipartFile.transferTo(uploadFile);
        } catch (IOException e) {
            //e.printStackTrace();
            throw e;
        }

        JsonResult jsonResult = new JsonResult();

        // 返回状态:1 表示上传成功
        jsonResult.setCode("1");
        // 返回服务器保存后的文件名
        jsonResult.put("fileName",targetFile);
        return jsonResult.getDataMap();

    }

    /**
     * 阿里云对象存储技术
     * @param multipartFile
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/uploadOss")
    public Map<String, Object> uploadOss(@RequestParam("file") MultipartFile[] multipartFile, HttpServletRequest request) throws Exception {
        JsonResult jsonResult = new JsonResult();
        for (MultipartFile file : multipartFile) {
            //QQ截图20200724153059.png
            String originalFilename = file.getOriginalFilename();
            System.out.println("originalFilename:"+originalFilename);
            // 从原始文件名中获取后缀,作为新添加的文件的文件名后缀(匹配)
            String subfix = originalFilename.substring(originalFilename.indexOf(".")+1);
            System.out.println("subfix:"+subfix);
            //使用UUID作为文件名,保证服务器中存放的文件名唯一

            String targetFile = UUID.randomUUID().toString().replace("-","")+"."+subfix;
            System.out.println("targetFile:"+targetFile);


            String fileName = "images/ComplaintPhoto/"+targetFile;

            System.out.println("fileName:"+fileName);

            //核心操作(调用OSS)

            OOSUtil.upload(fileName,file.getInputStream());



            // 返回状态:1 表示上传成功
            jsonResult.setCode("1");
            // 返回服务器保存后的文件名
            jsonResult.put("fileName",fileName);


        }
        System.out.println("dataMap:"+jsonResult.getDataMap());
        return jsonResult.getDataMap();

    }
}
